<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="laravel-homestead" tabindex="-1"><a class="header-anchor" href="#laravel-homestead"><span>Laravel Homestead</span></a></h2>
<ul>
<li><a href="#introduction">简介</a></li>
<li><a href="#installation-and-setup">安装与设置</a>
<ul>
<li><a href="#first-steps">第一步</a></li>
<li><a href="#configuring-homestead">配置 Homestead</a></li>
<li><a href="#configuring-nginx-sites">配置 Nginx 站点</a></li>
<li><a href="#configuring-services">配置服务</a></li>
<li><a href="#launching-the-vagrant-box">启动 Vagrant Box</a></li>
<li><a href="#per-project-installation">为项目单独安装</a></li>
<li><a href="#installing-optional-features">安装可选功能</a></li>
<li><a href="#aliases">别名</a></li>
</ul>
</li>
<li><a href="#updating-homestead">更新 Homestead</a></li>
<li><a href="#daily-usage">日常使用方法</a>
<ul>
<li><a href="#connecting-via-ssh">通过 SSH 连接</a></li>
<li><a href="#adding-additional-sites">添加其他站点</a></li>
<li><a href="#environment-variables">环境变量</a></li>
<li><a href="#ports">端口</a></li>
<li><a href="#php-versions">多 PHP 版本</a></li>
<li><a href="#connecting-to-databases">连接数据库</a></li>
<li><a href="#database-backups">数据库备份</a></li>
<li><a href="#configuring-cron-schedules">配置 Cron 调度器</a></li>
<li><a href="#configuring-mailpit">配置 MailHog</a></li>
<li><a href="#configuring-minio">配置 Minio</a></li>
<li><a href="#laravel-dusk">Laravel Dusk</a></li>
<li><a href="#sharing-your-environment">共享你的环境</a></li>
</ul>
</li>
<li><a href="#debugging-and-profiling">调试与性能分析</a>
<ul>
<li><a href="#debugging-web-requests">使用 Xdebug 调试 Web 请求</a></li>
<li><a href="#debugging-cli-applications">调试 CLI 应用程序</a></li>
<li><a href="#profiling-applications-with-blackfire">使用 Blackfire 为应用程序分析性能</a></li>
</ul>
</li>
<li><a href="#network-interfaces">网络接口</a></li>
<li><a href="#extending-homestead">扩展 Homestead</a></li>
<li><a href="#provider-specific-settings">针对虚拟机软件的特殊设置</a>
<ul>
<li><a href="#provider-specific-virtualbox">VirtualBox</a></li>
</ul>
</li>
</ul>
<h2 id="简介" tabindex="-1"><a class="header-anchor" href="#简介"><span>简介</span></a></h2>
<p>Laravel 致力于让整个 PHP 开发体验变得愉悦，包括您的本地开发环境。 <a href="https://github.com/laravel/homestead" target="_blank" rel="noopener noreferrer">Laravel Homestead</a> 是一个官方的预封装的 Vagrant box 套件，它为你提供了一个绝佳的开发环境，而无需你在本地机器上安装 PHP、Web 服务器及任何其他服务器软件。</p>
<p><a href="https://www.vagrantup.com/" target="_blank" rel="noopener noreferrer">Vagrant</a> 提供了一种简单、优雅的方式来管理和配置虚拟机。 Vagrant Box 完全是一次性的。如果出现问题，你可以在几分钟内销毁并重新创建 Box !</p>
<p>Homestead 可以在任何 Windows、 macOS 或 Linux 系统上运行，它预装好了 Nginx、 PHP、 MySQL、 PostgreSQL、 Redis、 Memcached、 Node 以及开发令人惊叹的 Laravel 应用程序所需的所有其他软件。</p>
<blockquote>
<p>[!注意]<br>
如果你使用的是 Windows ，你可能需要启用硬件虚拟化（ VT-x ）。该功能通常需要通过你的 BIOS 启用。如果你在 UEFI 系统上使用 Hyper-V ，则可能还需要禁用 Hyper-V 才能访问 VT-x 。</p>
</blockquote>
<h3 id="内置软件" tabindex="-1"><a class="header-anchor" href="#内置软件"><span>内置软件</span></a></h3>
<ul>
<li>Ubuntu 22.04</li>
<li>Git</li>
<li>PHP 8.3</li>
<li>PHP 8.2</li>
<li>PHP 8.1</li>
<li>PHP 8.0</li>
<li>PHP 7.4</li>
<li>PHP 7.3</li>
<li>PHP 7.2</li>
<li>PHP 7.1</li>
<li>PHP 7.0</li>
<li>PHP 5.6</li>
<li>Nginx</li>
<li>MySQL 8.0</li>
<li>lmm</li>
<li>Sqlite3</li>
<li>PostgreSQL 15</li>
<li>Composer</li>
<li>Docker</li>
<li>Node (With Yarn, Bower, Grunt, and Gulp)</li>
<li>Redis</li>
<li>Memcached</li>
<li>Beanstalkd</li>
<li>Mailpit</li>
<li>avahi</li>
<li>ngrok</li>
<li>Xdebug</li>
<li>XHProf / Tideways / XHGui</li>
<li>wp-cli</li>
</ul>
<h3 id="可选软件" tabindex="-1"><a class="header-anchor" href="#可选软件"><span>可选软件</span></a></h3>
<ul>
<li>Apache</li>
<li>Blackfire</li>
<li>Cassandra</li>
<li>Chronograf</li>
<li>CouchDB</li>
<li>Crystal &amp; Lucky Framework</li>
<li>Elasticsearch</li>
<li>EventStoreDB</li>
<li>Flyway</li>
<li>Gearman</li>
<li>Go</li>
<li>Grafana</li>
<li>InfluxDB</li>
<li>Logstash</li>
<li>MariaDB</li>
<li>Meilisearch</li>
<li>MinIO</li>
<li>MongoDB</li>
<li>Neo4j</li>
<li>Oh My Zsh</li>
<li>Open Resty</li>
<li>PM2</li>
<li>Python</li>
<li>R</li>
<li>RabbitMQ</li>
<li>Rust</li>
<li>RVM (Ruby Version Manager)</li>
<li>Solr</li>
<li>TimescaleDB</li>
<li>Trader (PHP extension)</li>
<li>Webdriver &amp; Laravel Dusk Utilities</li>
</ul>
<h2 id="安装与设置" tabindex="-1"><a class="header-anchor" href="#安装与设置"><span>安装与设置</span></a></h2>
<h3 id="第一步" tabindex="-1"><a class="header-anchor" href="#第一步"><span>第一步</span></a></h3>
<p>在启动 Homestead 环境之前，你必须安装 <a href="https://developer.hashicorp.com/vagrant/downloads" target="_blank" rel="noopener noreferrer">Vagrant</a> 及以下受支持的虚拟机之一:</p>
<ul>
<li><a href="https://www.virtualbox.org/wiki/Download_Old_Builds_6_1" target="_blank" rel="noopener noreferrer">VirtualBox 6.1.x</a></li>
<li><a href="https://www.parallels.com/products/desktop/" target="_blank" rel="noopener noreferrer">Parallels</a></li>
</ul>
<p>所有这些软件包都为所有常用操作系统提供了易于使用的可视化安装程序。</p>
<p>如果要使用 Parallels 提供虚拟机服务, 你需要安装 <a href="https://github.com/Parallels/vagrant-parallels" target="_blank" rel="noopener noreferrer">Parallels Vagrant plug-in</a>。这个插件是免费的。</p>
<h4 id="安装-homestead" tabindex="-1"><a class="header-anchor" href="#安装-homestead"><span>安装 Homestead</span></a></h4>
<p>你可以通过将 Homestead 仓库克隆到你的主机上来安装 Homestead。建议将仓库克隆到<code v-pre>home</code>下的<code v-pre>Homestead</code> 文件夹中，因为 Homestead 虚拟机将作为你所有 Laravel 应用程序的主机。在本文档中，我们将这个目录称为你的「Homestead 目录」：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">git</span> clone https://github.com/laravel/homestead.git ~/Homestead</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>克隆 Laravel Homestead 仓库后，你应该切换到<code v-pre>release</code> 分支。 这个分支总是包含 Homestead 的最新稳定版本：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token builtin class-name">cd</span> ~/Homestead</span>
<span class="line"></span>
<span class="line"><span class="token function">git</span> checkout release</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>接下来，从 Homestead 目录执行 <code v-pre>bash init.sh</code> 命令以创建 <code v-pre>Homestead.yaml</code> 配置文件。 <code v-pre>Homestead.yaml</code> 文件是你为 Homestead 安装配置所有设置的地方。 这个文件将被放置在 Homestead 目录中：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token comment"># macOS / Linux...</span></span>
<span class="line"><span class="token function">bash</span> init.sh</span>
<span class="line"></span>
<span class="line"><span class="token comment"># Windows...</span></span>
<span class="line">init.bat</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="配置-homestead" tabindex="-1"><a class="header-anchor" href="#配置-homestead"><span>配置 Homestead</span></a></h3>
<h4 id="设置提供服务的虚拟机程序" tabindex="-1"><a class="header-anchor" href="#设置提供服务的虚拟机程序"><span>设置提供服务的虚拟机程序</span></a></h4>
<p><code v-pre>Homestead.yaml</code> 文件中的 <code v-pre>provider</code>键的值用来配置使用哪个 Vagrant 提供虚拟机服务： <code v-pre>virtualbox</code> 或 <code v-pre>parallels</code>:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">provider<span class="token punctuation">:</span> virtualbox</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><blockquote>
<p>[!注意]<br>
如果你使用的是 Apple Silicon，那么你需要使用 Parallels 虚拟机。</p>
</blockquote>
<h4 id="配置共享文件夹" tabindex="-1"><a class="header-anchor" href="#配置共享文件夹"><span>配置共享文件夹</span></a></h4>
<p><code v-pre>Homestead.yaml</code> 文件的<code v-pre>folders</code> 属性列出了你希望与 Homestead 环境共享的所有文件夹。 当这些文件夹中的文件发生更改时，它们将在你的本地机器和 Homestead 虚拟环境之间保持同步。 你可以根据需要配置任意数量的共享文件夹：</p>
<div class="language-ini line-numbers-mode" data-highlighter="prismjs" data-ext="ini" data-title="ini"><pre v-pre class="language-ini"><code><span class="line">folders:</span>
<span class="line">    - map: ~/code/project1</span>
<span class="line">      to: /home/vagrant/project1</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>[!注意]<br>
Windows 用户不应使用 <code v-pre>~/</code> 路径语法，而应使用其项目的完整路径，例如 <code v-pre>C:\Users\user\Code\project1</code>.</p>
</blockquote>
<p>你应该始终将单个应用程序映射到它们自己的文件夹映射，而不是映射一个包含所有应用程序的单一大型目录。当你映射一个文件夹时，虚拟机必须跟踪该文件夹中每个文件的所有磁盘 I/O。如果你的文件夹中有大量文件，性能可能会下降：</p>
<div class="language-ini line-numbers-mode" data-highlighter="prismjs" data-ext="ini" data-title="ini"><pre v-pre class="language-ini"><code><span class="line">folders:</span>
<span class="line">    - map: ~/code/project1</span>
<span class="line">      to: /home/vagrant/project1</span>
<span class="line">    - map: ~/code/project2</span>
<span class="line">      to: /home/vagrant/project2</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>[!注意]<br>
在使用 Homestead 时，你永远不应该挂载<code v-pre>.</code> (当前目录) 。 这会导致 Vagrant 不会将当前文件夹映射到 <code v-pre>/vagrant</code>，并且会在配置时破坏可选功能并导致意外结果。</p>
</blockquote>
<p>要启用 <a href="https://developer.hashicorp.com/vagrant/docs/synced-folders/nfs" target="_blank" rel="noopener noreferrer">NFS</a>，你可以在文件夹映射中添加一个 <code v-pre>type</code> 选项：</p>
<div class="language-ini line-numbers-mode" data-highlighter="prismjs" data-ext="ini" data-title="ini"><pre v-pre class="language-ini"><code><span class="line">folders:</span>
<span class="line">    - map: ~/code/project1</span>
<span class="line">      to: /home/vagrant/project1</span>
<span class="line">      type: "nfs"</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>[!注意]<br>
在 Windows 上使用 NFS 时，应考虑安装 <a href="https://github.com/winnfsd/vagrant-winnfsd" target="_blank" rel="noopener noreferrer">vagrant-winnfsd</a> 插件。 该插件将维护 Homestead 虚拟机中文件和目录的正确用户/组权限。</p>
</blockquote>
<p>你还可以通过配置<code v-pre>options</code>属性列出它们来传递 Vagrant 的<a href="https://developer.hashicorp.com/vagrant/docs/synced-folders/basic_usage" target="_blank" rel="noopener noreferrer">同步文件夹</a> 支持的任何选项：</p>
<div class="language-ini line-numbers-mode" data-highlighter="prismjs" data-ext="ini" data-title="ini"><pre v-pre class="language-ini"><code><span class="line">folders:</span>
<span class="line">    - map: ~/code/project1</span>
<span class="line">      to: /home/vagrant/project1</span>
<span class="line">      type: "rsync"</span>
<span class="line">      options:</span>
<span class="line">          rsync__args: ["--verbose", "--archive", "--delete", "-zz"]</span>
<span class="line">          rsync__exclude: ["node_modules"]</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="配置-nginx-站点" tabindex="-1"><a class="header-anchor" href="#配置-nginx-站点"><span>配置 Nginx 站点</span></a></h3>
<p>不熟悉 Nginx？ 没问题。 你的 <code v-pre>Homestead.yaml</code> 文件的 <code v-pre>sites</code> 属性允许你轻松地将「域」映射到 Homestead 环境中的文件夹。 <code v-pre>Homestead.yaml</code> 文件中包含一个示例站点配置。 同样，你可以根据需要向 Homestead 环境添加任意数量的站点。 Homestead 可以为你正在开发的每个 Laravel 应用程序提供方便的虚拟化环境：</p>
<div class="language-ini line-numbers-mode" data-highlighter="prismjs" data-ext="ini" data-title="ini"><pre v-pre class="language-ini"><code><span class="line">sites:</span>
<span class="line">    - map: homestead.test</span>
<span class="line">      to: /home/vagrant/project1/public</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你在配置 Homestead 虚拟机后更改了 <code v-pre>sites</code> 属性，你应该在终端中执行 <code v-pre>vagrant reload --provision</code> 命令来更新虚拟机上的 Nginx 配置。</p>
<blockquote>
<p>[!注意]<br>
脚本被设计得尽可能具有幂等性。但是，如果你在配置过程中遇到问题，你应该通过执行 <code v-pre>vagrant destroy &amp;&amp; vagrant up</code> 命令来销毁和重建机器。</p>
</blockquote>
<h4 id="主机名解析" tabindex="-1"><a class="header-anchor" href="#主机名解析"><span>主机名解析</span></a></h4>
<p>Homestead 使用 <code v-pre>mDNS</code> 发布主机名以进行自动主机解析。 如果你在 <code v-pre>hostname: homestead</code> 文件中设置 <code v-pre>Homestead.yaml</code> ，主机将在 <code v-pre>homestead.local</code>中可用。 macOS、iOS 和 Linux 桌面发行版默认包含 <code v-pre>mDNS</code> 支持。 如果你使用的是 Windows，则必须安装 <a href="https://support.apple.com/kb/DL999?viewlocale=en_US&amp;locale=en_US" target="_blank" rel="noopener noreferrer">Bonjour Print Services for Windows</a>。</p>
<p>使用自动主机名最适合 Homestead 的 <a href="#per-project-installation">每个项目安装</a> 如果你在单个 Homestead 实例上托管多个站点，你可以将你网站的「域名」添加到你机器上的 <code v-pre>hosts</code> 文件中。 <code v-pre>hosts</code>文件会将你对 Homestead 站点的请求重定向到你的 Homestead 虚拟机中。 在 macOS 和 Linux 上，此文件位于 <code v-pre>/etc/hosts</code>. 在 Windows 上，它位于 <code v-pre>C:\Windows\System32\drivers\etc\hosts</code> 。 你添加到此文件的行将如下所示：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token number">192.168</span><span class="token number">.56</span><span class="token number">.56</span>  homestead<span class="token operator">.</span>test</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>确保列出的 IP 地址是你在 <code v-pre>Homestead.yaml</code>文件中设置的地址。将域名添加到 <code v-pre>hosts</code> 文件并启动 Vagrant 盒子后，你将能够通过 Web 浏览器访问该站点：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">http://homestead.test</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="配置服务" tabindex="-1"><a class="header-anchor" href="#配置服务"><span>配置服务</span></a></h3>
<p>Homestead 默认会启动好几个服务； 但你可以在配置的时候自定义启用或禁用哪些服务。 例如，你可以通过修改 <code v-pre>Homestead.yaml</code> 文件中的 <code v-pre>services</code> 选项来启用 PostgreSQL 并禁用 MySQL：</p>
<div class="language-ini line-numbers-mode" data-highlighter="prismjs" data-ext="ini" data-title="ini"><pre v-pre class="language-ini"><code><span class="line">services:</span>
<span class="line">    - enabled:</span>
<span class="line">        - "postgresql"</span>
<span class="line">    - disabled:</span>
<span class="line">        - "mysql"</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>指定的服务将根据它们在 <code v-pre>enabled</code> 和 <code v-pre>disabled</code> 指令中的顺序来启动或停止。</p>
<h3 id="启动-the-vagrant-box" tabindex="-1"><a class="header-anchor" href="#启动-the-vagrant-box"><span>启动 The Vagrant Box</span></a></h3>
<p>你根据自己的需求修改 <code v-pre>Homestead.yaml</code> 后，你可以通过在 <code v-pre>Homestead</code> 目录运行 <code v-pre>vagrant up</code> 命令来启动 Vagrant 虚拟机。 Vagrant 将启动虚拟机并自动配置你的共享文件夹和 Nginx 站点。</p>
<p>要销毁虚拟机实例，你可以使用 <code v-pre>vagrant destroy</code> 命令。</p>
<h3 id="为项目单独安装" tabindex="-1"><a class="header-anchor" href="#为项目单独安装"><span>为项目单独安装</span></a></h3>
<p>你可以为你管理的每个项目配置一个 <code v-pre>Homestead</code> 实例，而不是全局安装 <code v-pre>Homestead</code> 并在所有项目中共享相同的 Homestead 虚拟机。 如果你希望随项目一起提供 <code v-pre>Vagrantfile</code>，允许其他人在克隆项目的存储库后立即 <code v-pre>vagrant up</code>，则为每个项目安装 Homestead 可能会有所帮助。</p>
<p>你可以使用 Composer 包管理器将 Homestead 安装到你的项目中：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">composer</span> require laravel/homestead <span class="token parameter variable">--dev</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>安装 Homestead 后，调用 Homestead 的 <code v-pre>make</code> 命令为你的项目生成 <code v-pre>Vagrantfile</code> 和 <code v-pre>Homestead.yaml</code> 文件。 这些文件将放置在项目的根目录中。 <code v-pre>make</code> 命令将自动配置 <code v-pre>Homestead.yaml</code> 文件中的站点和文件夹指令：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token comment"># macOS / Linux...</span></span>
<span class="line">php vendor/bin/homestead <span class="token function">make</span></span>
<span class="line"></span>
<span class="line"><span class="token comment"># Windows...</span></span>
<span class="line">vendor<span class="token punctuation">\</span><span class="token punctuation">\</span>bin<span class="token punctuation">\</span><span class="token punctuation">\</span>homestead <span class="token function">make</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>接下来，在你的终端中运行 <code v-pre>vagrant up</code> 命令，并在浏览器中通过 <code v-pre>http://homestead.test</code> 访问你的项目。请记住，如果你没有使用自动的<a href="#hostname-resolution">主机名解析</a>，你仍然需要在 <code v-pre>/etc/hosts</code> 文件中为 <code v-pre>homestead.test</code> 文件中添加一个主机名映射。</p>
<h3 id="安装可选功能" tabindex="-1"><a class="header-anchor" href="#安装可选功能"><span>安装可选功能</span></a></h3>
<p>使用 <code v-pre>Homestead.yaml</code> 文件中的 <code v-pre>features</code> 选项可以安装可选软件。 大多数功能可以使用布尔值启用或禁用，部分功能允许使用多个配置选项：</p>
<div class="language-ini line-numbers-mode" data-highlighter="prismjs" data-ext="ini" data-title="ini"><pre v-pre class="language-ini"><code><span class="line">features:</span>
<span class="line">    - blackfire:</span>
<span class="line">        server_id: "server_id"</span>
<span class="line">        server_token: "server_value"</span>
<span class="line">        client_id: "client_id"</span>
<span class="line">        client_token: "client_value"</span>
<span class="line">    - cassandra: true</span>
<span class="line">    - chronograf: true</span>
<span class="line">    - couchdb: true</span>
<span class="line">    - crystal: true</span>
<span class="line">    - dragonflydb: true</span>
<span class="line">    - elasticsearch:</span>
<span class="line">        version: 7.9.0</span>
<span class="line">    - eventstore: true</span>
<span class="line">        version: 21.2.0</span>
<span class="line">    - flyway: true</span>
<span class="line">    - gearman: true</span>
<span class="line">    - golang: true</span>
<span class="line">    - grafana: true</span>
<span class="line">    - influxdb: true</span>
<span class="line">    - logstash: true</span>
<span class="line">    - mariadb: true</span>
<span class="line">    - meilisearch: true</span>
<span class="line">    - minio: true</span>
<span class="line">    - mongodb: true</span>
<span class="line">    - neo4j: true</span>
<span class="line">    - ohmyzsh: true</span>
<span class="line">    - openresty: true</span>
<span class="line">    - pm2: true</span>
<span class="line">    - python: true</span>
<span class="line">    - r-base: true</span>
<span class="line">    - rabbitmq: true</span>
<span class="line">    - rustc: true</span>
<span class="line">    - rvm: true</span>
<span class="line">    - solr: true</span>
<span class="line">    - timescaledb: true</span>
<span class="line">    - trader: true</span>
<span class="line">    - webdriver: true</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="elasticsearch" tabindex="-1"><a class="header-anchor" href="#elasticsearch"><span>Elasticsearch</span></a></h4>
<p>你可以指定一个受支持的 Elasticsearch 版本，版本号必须是一个确切的版本号（主版本号.次版本号.补丁版本号）。默认安装将创建一个名为 'homestead' 的集群。你不应该给 Elasticsearch 分配超过操作系统内存的一半，所以请确保你的 Homestead 虚拟机至少有 Elasticsearch 分配量的两倍内存。</p>
<blockquote>
<p>[!注意]<br>
点击查看 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current" target="_blank" rel="noopener noreferrer">Elasticsearch documentation</a> 学习如何自定义你的配置。</p>
</blockquote>
<h4 id="mariadb" tabindex="-1"><a class="header-anchor" href="#mariadb"><span>MariaDB</span></a></h4>
<p>启用 MariaDB 将会移除 MySQL 并安装 MariaDB。MariaDB 通常是用于替代 MySQL ，完全兼容 MySQL，所以在应用数据库配置中你仍然可以使用 <code v-pre>mysql</code> 驱动。</p>
<h4 id="mongodb" tabindex="-1"><a class="header-anchor" href="#mongodb"><span>MongoDB</span></a></h4>
<p>默认安装的 MongoDB 将会设置数据库用户名为 <code v-pre>homestead</code> 及对应的密码为 <code v-pre>secret</code>。</p>
<h4 id="neo4j" tabindex="-1"><a class="header-anchor" href="#neo4j"><span>Neo4j</span></a></h4>
<p>Neo4j 是一个图形数据库，默认安装的 Neo4j 会设置数据库用户名为 <code v-pre>homestead</code> 及对应的密码 <code v-pre>secret</code>。要通过浏览器访问 Neo4j ，请通过 Web 浏览器访问 <code v-pre>http://homestead.test:7474</code>。默认情况下，服务预设了端口 <code v-pre>7687</code>（Bolt）、<code v-pre>7474</code>（HTTP）和 <code v-pre>7473</code>（HTTPS）为来自 Neo4j 客户端的请求提供服务。</p>
<h3 id="系统命令别名" tabindex="-1"><a class="header-anchor" href="#系统命令别名"><span>系统命令别名</span></a></h3>
<p>您可以通过修改 Homestead 目录中的 <code v-pre>aliases</code> 文件将 Bash 命令别名添加到 Homestead 虚拟机：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token builtin class-name">alias</span> <span class="token assign-left variable">c</span><span class="token operator">=</span><span class="token string">'clear'</span></span>
<span class="line"><span class="token builtin class-name">alias</span> <span class="token punctuation">..</span><span class="token operator">=</span><span class="token string">'cd ..'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><p>当你更新完 <code v-pre>aliases</code> 文件后，你需要通过 <code v-pre>vagrant reload --provision</code> 命令重启 Homestead 机器，以确保新的别名在机器上生效。</p>
<h2 id="更新-homestead" tabindex="-1"><a class="header-anchor" href="#更新-homestead"><span>更新 Homestead</span></a></h2>
<p>更新 Homestead 之前确保你已经在 Homestead 目录下通过如下命令移除了当前的虚拟机：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">vagrant destroy</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>接下来，需要更新 Homestead 源码，如果你已经克隆仓库到本地，可以在项目根目录下运行如下命令进行更新：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">git</span> fetch</span>
<span class="line"></span>
<span class="line"><span class="token function">git</span> pull origin release</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>这些命令会从 Github 存储库中拉取最新的 Homestead 仓库代码到本地，包括最新的标签版本。你可以在 Homestead 的 <a href="https://github.com/laravel/homestead/releases" target="_blank" rel="noopener noreferrer">GitHub 发布页面</a> 上找到最新的稳定版本。</p>
<p>如果你是通过 Composer 在指定 Laravel 项目中安装的 Homestead，需要确保 <code v-pre>composer.json</code> 中包含了 <code v-pre>&quot;laravel/homestead&quot;: &quot;^12&quot;</code>，然后更新这个依赖：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">composer</span> update</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>之后，你需要通过 <code v-pre>vagrant box update</code> 命令更新 Vagrant：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">vagrant box update</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>接下来，你可以从 Homestead 目录下运行 <code v-pre>bash init.sh</code> 命令来更新 Homestead 额外的配置文件，你会被询问是否覆盖已存在的 <code v-pre>Homestead.yaml</code>、<code v-pre>after.sh</code> 以及 <code v-pre>aliases</code> 文件：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token comment"># macOS / Linux...</span></span>
<span class="line"><span class="token function">bash</span> init.sh</span>
<span class="line"></span>
<span class="line"><span class="token comment"># Windows...</span></span>
<span class="line">init.bat</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>最后，你需要重新生成新的 Homestead 虚拟机来使用最新安装的 Vagrant：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">vagrant up</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="日常使用方法" tabindex="-1"><a class="header-anchor" href="#日常使用方法"><span>日常使用方法</span></a></h2>
<h3 id="通过-ssh-连接" tabindex="-1"><a class="header-anchor" href="#通过-ssh-连接"><span>通过 SSH 连接</span></a></h3>
<p>你可以在 Homestead 目录下通过运行 <code v-pre>vagrant ssh</code> 以 SSH 方式连接到虚拟机。如果你设置了全部访问 Homestead，也可以在任意路径下通过 homestead ssh 登录到虚拟机。</p>
<h3 id="添加其他站点" tabindex="-1"><a class="header-anchor" href="#添加其他站点"><span>添加其他站点</span></a></h3>
<p>Homestead 虚拟机在运行时，可能需要添加多个 Laravel 应用到 Nginx 站点。如果是在单个 Homestead 环境中运行多个 Laravel 应用，添加站点很简单，只需将站点添加到 <code v-pre>Homestead.yaml</code> 文件：</p>
<div class="language-ini line-numbers-mode" data-highlighter="prismjs" data-ext="ini" data-title="ini"><pre v-pre class="language-ini"><code><span class="line">sites:</span>
<span class="line">    - map: homestead.test</span>
<span class="line">      to: /home/vagrant/project1/public</span>
<span class="line">    - map: another.test</span>
<span class="line">      to: /home/vagrant/project2/public</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>[!注意]<br>
在添加站点之前，你应该确保已经为项目的目录配置了 <a href="#configuring-shared-folders">配置共享文件夹</a>。</p>
</blockquote>
<p>如果 Vagrant 没有自动管理你的「hosts」文件，你可能还需要将新站点添加到该文件中。在 macOS 和 Linux 上，此文件位于 <code v-pre>/etc/hosts</code>。在 Windows 上，它位于 <code v-pre>C:\Windows\System32\drivers\etc\hosts</code>:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token number">192.168</span><span class="token number">.56</span><span class="token number">.56</span>  homestead<span class="token operator">.</span>test</span>
<span class="line"><span class="token number">192.168</span><span class="token number">.56</span><span class="token number">.56</span>  another<span class="token operator">.</span>test</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><p>添加站点后，你需要在 Homestead 目录运行 <code v-pre>vagrant reload --provision</code> 命令才可以加载新的站点。</p>
<h4 id="站点类型" tabindex="-1"><a class="header-anchor" href="#站点类型"><span>站点类型</span></a></h4>
<p>Homestead 支持多种「类型」的站点，让你可以轻松运行不是基于 Laravel 的项目。 例如，我们可以使用 <code v-pre>statamic</code> 站点类型轻松地将 Statamic 应用程序添加到 Homestead：</p>
<div class="language-ini line-numbers-mode" data-highlighter="prismjs" data-ext="ini" data-title="ini"><pre v-pre class="language-ini"><code><span class="line">sites:</span>
<span class="line">    - map: statamic.test</span>
<span class="line">      to: /home/vagrant/my-symfony-project/web</span>
<span class="line">      type: "statamic"</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>可用的站点类型有：<code v-pre>apache</code>, <code v-pre>apache-proxy</code>, <code v-pre>apigility</code>, <code v-pre>expressive</code>, <code v-pre>laravel</code> (默认), <code v-pre>proxy</code> (用于 nginx ), <code v-pre>silverstripe</code>, <code v-pre>statamic</code>, <code v-pre>symfony2</code>, <code v-pre>symfony4</code>, 和 <code v-pre>zf</code>。</p>
<h4 id="站点参数" tabindex="-1"><a class="header-anchor" href="#站点参数"><span>站点参数</span></a></h4>
<p>你可以通过 <code v-pre>params</code> 站点指令向你的站点添加额外的 Nginx <code v-pre>fastcgi_param</code> 值：</p>
<div class="language-ini line-numbers-mode" data-highlighter="prismjs" data-ext="ini" data-title="ini"><pre v-pre class="language-ini"><code><span class="line">sites:</span>
<span class="line">    - map: homestead.test</span>
<span class="line">      to: /home/vagrant/project1/public</span>
<span class="line">      params:</span>
<span class="line">          - key: FOO</span>
<span class="line">            value: BAR</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="环境变量" tabindex="-1"><a class="header-anchor" href="#环境变量"><span>环境变量</span></a></h3>
<p>你可以 <code v-pre>Homestead.yaml</code> 文件来定义全局环境变量：</p>
<div class="language-ini line-numbers-mode" data-highlighter="prismjs" data-ext="ini" data-title="ini"><pre v-pre class="language-ini"><code><span class="line">variables:</span>
<span class="line">    - key: APP_ENV</span>
<span class="line">      value: local</span>
<span class="line">    - key: FOO</span>
<span class="line">      value: bar</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>更新 <code v-pre>Homestead.yaml</code> 文件后，请务必通过执行 <code v-pre>vagrant reload --provision</code> 命令重新加载配置。 这将更新所有已安装 PHP 版本的 PHP-FPM 配置，并为 <code v-pre>vagrant</code> 用户更新环境。</p>
<h3 id="端口" tabindex="-1"><a class="header-anchor" href="#端口"><span>端口</span></a></h3>
<p>默认情况下，以下端口会转发到你的 Homestead 环境：</p>
<ul>
<li><strong>HTTP:</strong> 8000 → 转发到 80</li>
<li><strong>HTTPS:</strong> 44300 → 转发到 443</li>
</ul>
<h4 id="转发额外的端口" tabindex="-1"><a class="header-anchor" href="#转发额外的端口"><span>转发额外的端口</span></a></h4>
<p>如你所愿，你可以通过在你的 <code v-pre>Homestead.yaml</code> 文件中定义一个 <code v-pre>ports</code> 配置项来将额外的端口转发到 Vagrant 虚拟机。 更新 <code v-pre>Homestead.yaml</code> 文件后，请务必通过执行 <code v-pre>vagrant reload --provision</code> 命令重新载入虚拟机配置：</p>
<div class="language-ini line-numbers-mode" data-highlighter="prismjs" data-ext="ini" data-title="ini"><pre v-pre class="language-ini"><code><span class="line">ports:</span>
<span class="line">    - send: 50000</span>
<span class="line">      to: 5000</span>
<span class="line">    - send: 7777</span>
<span class="line">      to: 777</span>
<span class="line">      protocol: udp</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>以下是你可能希望从主机映射到 Vagrant box 的其他 Homestead 服务的端口清单：</p>
<ul>
<li><strong>SSH:</strong> 2222 → 转发到 22</li>
<li><strong>ngrok UI:</strong> 4040 → 转发到 4040</li>
<li><strong>MySQL:</strong> 33060 → 转发到 3306</li>
<li><strong>PostgreSQL:</strong> 54320 → 转发到 5432</li>
<li><strong>MongoDB:</strong> 27017 → 转发到 27017</li>
<li><strong>Mailpit:</strong> 8025 → 转发到 8025</li>
<li><strong>Minio:</strong> 9600 → 转发到 9600</li>
</ul>
<h3 id="多-php-版本" tabindex="-1"><a class="header-anchor" href="#多-php-版本"><span>多 PHP 版本</span></a></h3>
<p>Homestead 引入了对在同一虚拟机上运行多个版本的 PHP 的支持。 你可以在 Homestead.yaml 文件中指定用于特定站点的 PHP 版本。 可用的 PHP 版本有：「5.6」、「7.0」、「7.1」、「7.2」、「7.3」、「7.4」、「8.0」、「8.1」、「8.2」和「8.3」（默认）:</p>
<div class="language-ini line-numbers-mode" data-highlighter="prismjs" data-ext="ini" data-title="ini"><pre v-pre class="language-ini"><code><span class="line">sites:</span>
<span class="line">    - map: homestead.test</span>
<span class="line">      to: /home/vagrant/project1/public</span>
<span class="line">      php: "7.1"</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><a href="#connecting-via-ssh">在你的 Homestead 虚拟机中</a>,你可以通过 CLI 使用任何支持的 PHP 版本：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php5.6 artisan list</span>
<span class="line">php7.0 artisan list</span>
<span class="line">php7.1 artisan list</span>
<span class="line">php7.2 artisan list</span>
<span class="line">php7.3 artisan list</span>
<span class="line">php7.4 artisan list</span>
<span class="line">php8.0 artisan list</span>
<span class="line">php8.1 artisan list</span>
<span class="line">php8.2 artisan list</span>
<span class="line">php8.3 artisan list</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以通过在 Homestead 虚拟机中发出以下命令来更改 CLI 使用的默认 PHP 版本：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php56</span>
<span class="line">php70</span>
<span class="line">php71</span>
<span class="line">php72</span>
<span class="line">php73</span>
<span class="line">php74</span>
<span class="line">php80</span>
<span class="line">php81</span>
<span class="line">php82</span>
<span class="line">php83</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="连接到数据库" tabindex="-1"><a class="header-anchor" href="#连接到数据库"><span>连接到数据库</span></a></h3>
<p><code v-pre>Homestead</code> 开箱即用地为 MySQL 和 PostgreSQL 配置了一个 homestead 数据库。如果你想用宿主机的数据库客户端连接到 MySQL 或 PostgreSQL 数据库，你可以通过连接 <code v-pre>127.0.0.1</code> （本地网络）的 33060 端口（MySQL） 或 54320 端口（PostgreSQL）。 两个数据库的用户名和密码都是 <code v-pre>homestead</code>/<code v-pre>secret</code>。</p>
<blockquote>
<p>[!注意]<br>
你应该只在从主机连接到数据库时使用这些非标准端口。在你的 Laravel 应用程序的 <code v-pre>database</code> 配置文件中，你将使用默认的 3306 和 5432 端口，因为 Laravel 是在虚拟机内部运行的。</p>
</blockquote>
<h3 id="数据库备份" tabindex="-1"><a class="header-anchor" href="#数据库备份"><span>数据库备份</span></a></h3>
<p>当你的 Homestead 虚拟机被销毁时，Homestead 可以自动备份你的数据库。 要使用此功能，你必须使用 Vagrant 2.1.0 或更高版本。 如果你使用的是旧版本的 Vagrant，则必须安装 <code v-pre>vagrant-triggers</code> 插件。要启用自动数据库备份，请将以下行添加到你的 <code v-pre>Homestead.yaml</code> 文件中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">backup<span class="token punctuation">:</span> <span class="token constant boolean">true</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>配置完成后，当执行 <code v-pre>vagrant destroy</code> 命令时，Homestead 会将你的数据库导出到 <code v-pre>.backup/mysql_backup</code> 和 <code v-pre>.backup/postgres_backup</code> 目录。 如果你选择了 <a href="#per-project-installation">为项目单独安装</a> Homestead，你可以在项目安装 Homestead 的文件夹中找到这些目录，或者在你的项目根目录中找到它们。</p>
<h3 id="配置-cron-计划任务" tabindex="-1"><a class="header-anchor" href="#配置-cron-计划任务"><span>配置 Cron 计划任务</span></a></h3>
<p>Laravel 提供了一种便捷方式来满足 <a href="https://learnku.com/docs/laravel/11.x/scheduling" target="_blank" rel="noopener noreferrer">任务调度</a> ，通过 Artisan 命令 <code v-pre>schedule:run</code> 实现了定时运行（每分钟执行一次）。 <code v-pre>schedule:run</code> 命令将检查在 <code v-pre>App\Console\Kernel</code> 类中定义的作业计划，以确定要运行哪些计划任务。</p>
<p>如果你想为 Homestead 站点运行 <code v-pre>schedule:run</code> 命令，可以在定义站点时将 <code v-pre>schedule</code> 选项设置为 <code v-pre>true</code>：</p>
<div class="language-ini line-numbers-mode" data-highlighter="prismjs" data-ext="ini" data-title="ini"><pre v-pre class="language-ini"><code><span class="line">sites:</span>
<span class="line">    - map: homestead.test</span>
<span class="line">      to: /home/vagrant/project1/public</span>
<span class="line">      schedule: true</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>站点的 cron 作业将在 Homestead 虚拟机的 <code v-pre>/etc/cron.d</code> 目录中被定义。</p>
<h3 id="配置mailpit" tabindex="-1"><a class="header-anchor" href="#配置mailpit"><span>配置Mailpit</span></a></h3>
<p><a href="https://github.com/axllent/mailpit" target="_blank" rel="noopener noreferrer">Mailpit</a> 会在你本地开发的过程中拦截应用程序发送的电子邮件，而不是将邮件实际发送给收件人。如果要使用 Mailpit，你需要参考以下邮件配置并更新应用程序的 <code v-pre>.env</code> 文件：</p>
<div class="language-ini line-numbers-mode" data-highlighter="prismjs" data-ext="ini" data-title="ini"><pre v-pre class="language-ini"><code><span class="line"><span class="token key attr-name">MAIL_MAILER</span><span class="token punctuation">=</span><span class="token value attr-value">smtp</span></span>
<span class="line"><span class="token key attr-name">MAIL_HOST</span><span class="token punctuation">=</span><span class="token value attr-value">localhost</span></span>
<span class="line"><span class="token key attr-name">MAIL_PORT</span><span class="token punctuation">=</span><span class="token value attr-value">1025</span></span>
<span class="line"><span class="token key attr-name">MAIL_USERNAME</span><span class="token punctuation">=</span><span class="token value attr-value">null</span></span>
<span class="line"><span class="token key attr-name">MAIL_PASSWORD</span><span class="token punctuation">=</span><span class="token value attr-value">null</span></span>
<span class="line"><span class="token key attr-name">MAIL_ENCRYPTION</span><span class="token punctuation">=</span><span class="token value attr-value">null</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>配置 Mailpit 后，你可以通过 <code v-pre>http://localhost:8025</code>访问 Mailpit 看板。</p>
<h3 id="配置-minio" tabindex="-1"><a class="header-anchor" href="#配置-minio"><span>配置 Minio</span></a></h3>
<p><a href="https://github.com/minio/minio" target="_blank" rel="noopener noreferrer">Minio</a> 是一个具有 Amazon S3 兼容 API 的开源对象存储服务器。 要安装 Minio，请使用 <a href="#installing-optional-features">features</a> 部分中的以下配置选项更新你的 <code v-pre>Homestead.yaml</code> 文件</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">minio<span class="token punctuation">:</span> <span class="token constant boolean">true</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>默认情况下，Minio 在端口 9600 上可用。你可以通过访问 <code v-pre>http://localhost:9600</code> 访问 Minio 控制面板。 默认访问键是 <code v-pre>homestead</code>，而默认密钥是 <code v-pre>secretkey</code>。 访问 Minio 时，应始终使用区域 <code v-pre>us-east-1</code></p>
<p>为了使用 MinIO，请确保您的<code v-pre>.env</code>文件具有以下选项：</p>
<div class="language-ini line-numbers-mode" data-highlighter="prismjs" data-ext="ini" data-title="ini"><pre v-pre class="language-ini"><code><span class="line"><span class="token key attr-name">AWS_USE_PATH_STYLE_ENDPOINT</span><span class="token punctuation">=</span><span class="token value attr-value">true</span></span>
<span class="line"><span class="token key attr-name">AWS_ENDPOINT</span><span class="token punctuation">=</span><span class="token value attr-value">http://localhost:9600</span></span>
<span class="line"><span class="token key attr-name">AWS_ACCESS_KEY_ID</span><span class="token punctuation">=</span><span class="token value attr-value">homestead</span></span>
<span class="line"><span class="token key attr-name">AWS_SECRET_ACCESS_KEY</span><span class="token punctuation">=</span><span class="token value attr-value">secretkey</span></span>
<span class="line"><span class="token key attr-name">AWS_DEFAULT_REGION</span><span class="token punctuation">=</span><span class="token value attr-value">us-east-1</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>要配置 Minio 支持的「S3」存储桶，请在你的 <code v-pre>Homestead.yaml</code> 文件中添加 <code v-pre>buckets</code> 指令。 定义存储桶后，你应该在终端中执行 <code v-pre>vagrant reload --provision</code> 命令重载虚拟机:</p>
<div class="language-ini line-numbers-mode" data-highlighter="prismjs" data-ext="ini" data-title="ini"><pre v-pre class="language-ini"><code><span class="line">buckets:</span>
<span class="line">    - name: your-bucket</span>
<span class="line">      policy: public</span>
<span class="line">    - name: your-private-bucket</span>
<span class="line">      policy: none</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>支持的 <code v-pre>policy</code> 值包括： <code v-pre>none</code>, <code v-pre>download</code>, <code v-pre>upload</code>, 和 <code v-pre>public</code>.</p>
<h3 id="laravel-dusk-测试工具" tabindex="-1"><a class="header-anchor" href="#laravel-dusk-测试工具"><span>Laravel Dusk 测试工具</span></a></h3>
<p>为了在 Homestead 中运行 <a href="https://learnku.com/docs/laravel/11.x/dusk" target="_blank" rel="noopener noreferrer">Laravel Dusk</a> 测试，你应该在 Homestead 配置中启用 <a href="#installing-optional-features"><code v-pre>webdriver</code> 功能</a>:</p>
<div class="language-ini line-numbers-mode" data-highlighter="prismjs" data-ext="ini" data-title="ini"><pre v-pre class="language-ini"><code><span class="line">features:</span>
<span class="line">    - webdriver: true</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><p>启用 <code v-pre>webdriver</code> 功能后，你应该在终端中执行 <code v-pre>vagrant reload --provision</code> 命令重载虚拟机。</p>
<h3 id="共享你的环境" tabindex="-1"><a class="header-anchor" href="#共享你的环境"><span>共享你的环境</span></a></h3>
<p>有时，你可能希望与同事或客户分享你目前正在做的事情。 Vagrant 通过 <code v-pre>vagrant share</code> 命令内置了对此的支持； 但是，如果你在 <code v-pre>Homestead.yaml</code> 文件中配置了多个站点，这个功能将不可用。</p>
<p>为了解决这个问题，Homestead 包含了自己的 <code v-pre>share</code> 命令。 首先，通过 <code v-pre>vagrant ssh</code> <a href="#connecting-via-ssh">SSH 到你的 Homestead 虚拟机</a> 并执行 <code v-pre>share homestead.test</code> 命令。 此命令将从你的 <code v-pre>Homestead.yaml</code> 配置文件中共享 <code v-pre>homestead.test</code> 站点。 你可以将任何其他配置的站点替换为 <code v-pre>homestead.test</code>：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">share homestead.test</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>运行该命令后，你将看到一个 Ngrok 屏幕出现，其中包含活动日志和共享站点的可公开访问的 URL。 如果你想指定自定义区域、子域或其他 Ngrok 运行时选项，你可以将它们添加到你的 <code v-pre>share</code> 命令中：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">share homestead.test <span class="token parameter variable">-region</span><span class="token operator">=</span>eu <span class="token parameter variable">-subdomain</span><span class="token operator">=</span>laravel</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果你需要通过HTTPS而不是HTTP来分享内容，使用<code v-pre>sshare</code>命令而不是<code v-pre>share</code>命令。</p>
<blockquote>
<p>[!注意]<br>
请记住，Vagrant 本质上是不安全的，并且你在运行 <code v-pre>share</code> 命令时会将虚拟机暴露在互联网上。</p>
</blockquote>
<h2 id="调试和分析" tabindex="-1"><a class="header-anchor" href="#调试和分析"><span>调试和分析</span></a></h2>
<h3 id="使用-xdebug-调试-web-请求" tabindex="-1"><a class="header-anchor" href="#使用-xdebug-调试-web-请求"><span>使用 Xdebug 调试 Web 请求</span></a></h3>
<p>Homestead 支持使用 <a href="https://xdebug.org/" target="_blank" rel="noopener noreferrer">Xdebug</a> 进行步骤调试。例如，你可以在浏览器中访问一个页面，PHP 将连接到你的 IDE 以允许检查和修改正在运行的代码。</p>
<p>默认情况下，Xdebug 将自动运行并准备好接受连接。 如果需要在 CLI 上启用 Xdebug，请在 Homestead 虚拟机中执行 <code v-pre>sudo phpenmod xdebug</code> 命令。接下来，按照 IDE 的说明启用调试。最后，配置你的浏览器以使用扩展或 <a href="https://www.jetbrains.com/phpstorm/marklets/" target="_blank" rel="noopener noreferrer">bookmarklet</a> 触发 Xdebug。</p>
<blockquote>
<p>[!注意]<br>
Xdebug 导致 PHP 运行速度明显变慢。要禁用 Xdebug，请在 Homestead 虚拟机中运行 <code v-pre>sudo phpdismod xdebug</code> 并重新启动 FPM 服务。</p>
</blockquote>
<h4 id="自动启动-xdebug" tabindex="-1"><a class="header-anchor" href="#自动启动-xdebug"><span>自动启动 Xdebug</span></a></h4>
<p>在调试对 Web 服务器发出请求的功能测试时，自动启动调试通常比修改测试以通过自定义头或 Cookie 触发调试更为方便。要强制 Xdebug 自动启动，您需要在 Homestead 虚拟机中的<code v-pre>/etc/php/7.x/fpm/conf.d/20-xdebug.ini</code>文件中进行修改，并添加以下配置：</p>
<div class="language-ini line-numbers-mode" data-highlighter="prismjs" data-ext="ini" data-title="ini"><pre v-pre class="language-ini"><code><span class="line"><span class="token comment">; If Homestead.yaml contains a different subnet for the IP address, this address may be different...</span></span>
<span class="line"><span class="token key attr-name">xdebug.client_host</span> <span class="token punctuation">=</span> <span class="token value attr-value">192.168.10.1</span></span>
<span class="line"><span class="token key attr-name">xdebug.mode</span> <span class="token punctuation">=</span> <span class="token value attr-value">debug</span></span>
<span class="line"><span class="token key attr-name">xdebug.start_with_request</span> <span class="token punctuation">=</span> <span class="token value attr-value">yes</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="调试-cli-应用程序" tabindex="-1"><a class="header-anchor" href="#调试-cli-应用程序"><span>调试 CLI 应用程序</span></a></h3>
<p>要调试 PHP CLI 应用程序，请在 Homestead 虚拟机中使用 <code v-pre>xphp</code> shell 别名：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">xphp <span class="token operator">/</span>path<span class="token operator">/</span>to<span class="token operator">/</span>script</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="使用-blackfire-分析应用程序" tabindex="-1"><a class="header-anchor" href="#使用-blackfire-分析应用程序"><span>使用 Blackfire 分析应用程序</span></a></h3>
<p><a href="https://blackfire.io/docs/introduction" target="_blank" rel="noopener noreferrer">Blackfire</a> 是一个用于分析 Web 请求和 CLI 应用程序的服务。它提供了一个交互式的用户界面，该界面以调用图和时间线的形式展示性能分析数据。Blackfire 是为开发、测试和生产环境而构建的，对于最终用户来说没有额外的开销。此外，Blackfire 还提供了对代码和 <code v-pre>php.ini</code> 配置设置的性能、质量和安全检查。</p>
<p><a href="https://blackfire.io/docs/player/index" target="_blank" rel="noopener noreferrer">Blackfire Player</a>是一个开源的 Web 爬行、Web 测试和 Web 抓取应用程序，可以与 Blackfire 联合使用以编写分析场景的脚本。</p>
<p>要启用 Blackfire，请使用 Homestead 配置文件中的「features」配置项：</p>
<div class="language-ini line-numbers-mode" data-highlighter="prismjs" data-ext="ini" data-title="ini"><pre v-pre class="language-ini"><code><span class="line">features:</span>
<span class="line">    - blackfire:</span>
<span class="line">        server_id: "server_id"</span>
<span class="line">        server_token: "server_value"</span>
<span class="line">        client_id: "client_id"</span>
<span class="line">        client_token: "client_value"</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Blackfire 服务器凭据和客户端凭据需要使用 <a href="https://blackfire.io/signup" target="_blank" rel="noopener noreferrer">Blackfire 帐户</a>。 Blackfire 提供了多种选项来分析应用程序，包括 CLI 工具和浏览器扩展。 请查看 <a href="https://blackfire.io/docs/php/integrations/laravel/index" target="_blank" rel="noopener noreferrer">Blackfire 文档</a>以获取更多详细信息。</p>
<h2 id="网络接口" tabindex="-1"><a class="header-anchor" href="#网络接口"><span>网络接口</span></a></h2>
<p><code v-pre>Homestead.yaml</code> 文件的 <code v-pre>networks</code> 属性为你的 Homestead 虚拟机配置网络接口。 你可以根据需要配置任意数量的接口：</p>
<div class="language-ini line-numbers-mode" data-highlighter="prismjs" data-ext="ini" data-title="ini"><pre v-pre class="language-ini"><code><span class="line">networks:</span>
<span class="line">    - type: "private_network"</span>
<span class="line">      ip: "192.168.10.20"</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>要启用 <a href="https://www.vagrantup.com/docs/networking/public_network.html" target="_blank" rel="noopener noreferrer">桥接</a> 接口，请为将网络配置调整为 <code v-pre>bridge</code> 并将网络类型更改为 <code v-pre>public_network</code>：</p>
<div class="language-ini line-numbers-mode" data-highlighter="prismjs" data-ext="ini" data-title="ini"><pre v-pre class="language-ini"><code><span class="line">networks:</span>
<span class="line">    - type: "public_network"</span>
<span class="line">      ip: "192.168.10.20"</span>
<span class="line">      bridge: "en1: Wi-Fi (AirPort)"</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>要启用 <a href="https://developer.hashicorp.com/vagrant/docs/networking/public_network#dhcp" target="_blank" rel="noopener noreferrer">DHCP</a> 功能，你只需从配置中删除 <code v-pre>ip</code> 选项：</p>
<div class="language-ini line-numbers-mode" data-highlighter="prismjs" data-ext="ini" data-title="ini"><pre v-pre class="language-ini"><code><span class="line">networks:</span>
<span class="line">    - type: "public_network"</span>
<span class="line">      bridge: "en1: Wi-Fi (AirPort)"</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>要更新网络所使用的设备，您可以在网络的配置中添加一个<code v-pre>dev</code>选项。默认的<code v-pre>dev</code>值是<code v-pre>eth0</code>：</p>
<div class="language-ini line-numbers-mode" data-highlighter="prismjs" data-ext="ini" data-title="ini"><pre v-pre class="language-ini"><code><span class="line">networks:</span>
<span class="line">    - type: "public_network"</span>
<span class="line">      ip: "192.168.10.20"</span>
<span class="line">      bridge: "en1: Wi-Fi (AirPort)"</span>
<span class="line">      dev: "enp2s0"</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="扩展-homestead" tabindex="-1"><a class="header-anchor" href="#扩展-homestead"><span>扩展 Homestead</span></a></h2>
<p>你可以使用 Homestead 目录根目录中的 <code v-pre>after.sh</code> 脚本扩展 Homestead。 在此文件中，你可以添加正确配置和自定义虚拟机所需的任何 shell 命令。</p>
<p>当你自定义 Homestead 时，Ubuntu 可能会询问你是要保留软件包的原始配置还是使用新的配置文件覆盖它。 为了避免这种情况，你应该在安装软件包时使用以下命令，以避免覆盖 Homestead 之前编写的任何配置：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">sudo</span> <span class="token function">apt-get</span> <span class="token parameter variable">-y</span> <span class="token punctuation">\</span></span>
<span class="line">    <span class="token parameter variable">-o</span> Dpkg::Options::<span class="token operator">=</span><span class="token string">"--force-confdef"</span> <span class="token punctuation">\</span></span>
<span class="line">    <span class="token parameter variable">-o</span> Dpkg::Options::<span class="token operator">=</span><span class="token string">"--force-confold"</span> <span class="token punctuation">\</span></span>
<span class="line">    <span class="token function">install</span> package-name</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="用户自定义" tabindex="-1"><a class="header-anchor" href="#用户自定义"><span>用户自定义</span></a></h3>
<p>与你的团队一起使用 Homestead 时，你可能需要调整 Homestead 以更好地适应你的个人开发风格。 为此，你可以在 Homestead 目录（包含 <code v-pre>Homestead.yaml</code> 文件的同一目录）的根目录中创建一个 <code v-pre>user-customizations.sh</code> 文件。 在此文件中，你可以进行任何你想要的自定义； 但是 <code v-pre>user-customizations.sh</code> 不应受版本管理工具控制。</p>
<h2 id="针对虚拟机软件的特殊设置" tabindex="-1"><a class="header-anchor" href="#针对虚拟机软件的特殊设置"><span>针对虚拟机软件的特殊设置</span></a></h2>
<h3 id="virtualbox" tabindex="-1"><a class="header-anchor" href="#virtualbox"><span>VirtualBox</span></a></h3>
<h4 id="natdnshostresolver" tabindex="-1"><a class="header-anchor" href="#natdnshostresolver"><span><code v-pre>natdnshostresolver</code></span></a></h4>
<p>默认情况下，Homestead 将 <code v-pre>natdnshostresolver</code> 设置配置为 <code v-pre>on</code>。 这允许 Homestead 使用你的主机操作系统的 DNS 设置。 如果你想覆盖此行为，请将以下配置选项添加到你的 <code v-pre>Homestead.yaml</code> 文件中：</p>
<div class="language-ini line-numbers-mode" data-highlighter="prismjs" data-ext="ini" data-title="ini"><pre v-pre class="language-ini"><code><span class="line">provider: virtualbox</span>
<span class="line">natdnshostresolver: 'off'</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/11.x/homesteadmd/16720" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/ho...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/11.x/homesteadmd/16720" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/ho...</a></p>
</blockquote>
</div></template>


